package com.ama.controller;

import com.ama.dao.CostDao;
import com.ama.pojo.Cost;
import com.ama.pojo.Staff;
import com.ama.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class PayCostController {
    @Autowired
    CostDao costDao;

    /**
     * 1、如果shouldpay为《=0 设置coststate = 1
     * 2、更新cost
     * @param request
     * @param session
     * @return 跳转缴费成功
     */
    @RequestMapping("/payCost")
    public String payCost(HttpServletRequest request, HttpSession session) {
        Staff staff = (Staff) session.getAttribute("user");
        Double pay = Double.parseDouble(request.getParameter("pay"));
        Long costId = staff.getCostId();
        Cost cost = costDao.selectByPrimaryKey(costId);
        Date now = DateUtil.getNow();
        Double costShouldpay = cost.getCostShouldpay();
        cost.setCostPaydate(now);
        cost.setCostShouldpay(costShouldpay-pay);
        cost.setCostRecentpay(pay);
        cost.setCostLastpay(pay);
        if(cost.getCostShouldpay()<=0){
            cost.setCostState(1);
        }
        costDao.updateByPrimaryKeySelective(cost);
        return "redirect:/success/s_paysuc.jsp";
    }

}
